package com.dw.widget.slideshow.config.view;

import java.util.EventListener;

import com.dw.widget.slideshow.R;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HeaderView extends RelativeLayout {

	private Context mContext;
	
	private ImageButton bnBack;
	private TextView tvBack;
	private TextView tvTitle;
	
	private boolean bnBackVisible = true;
	private CharSequence backText;
	private CharSequence titleText;
	private ColorStateList titleColor;
	
	private OnBackButtonClickListener onBackButtonListener;
	
	public HeaderView(Context context, AttributeSet attrs, int defStyle) {
		
		super(context, attrs, defStyle);
		
		mContext = context;
		
		TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.header, defStyle, 0);
		
		bnBackVisible = a.getBoolean(R.styleable.header_backVisible, true);
		backText = a.getString(R.styleable.header_backText);
		titleText = a.getString(R.styleable.header_titleText);
		titleColor = a.getColorStateList(R.styleable.header_titleColor);
	}
	
	public HeaderView(Context context, AttributeSet attrs) {
		
		this(context, attrs, -1);
	}
	
	public HeaderView(Context context) {
		
		this(context, null);
	}

	@Override
	protected void onFinishInflate() {

		super.onFinishInflate();
		
		LayoutInflater.from(mContext).inflate(R.layout.header, this, true);
		
		bnBack = (ImageButton)findViewById(R.id.header_backbn);
		tvBack = (TextView)findViewById(R.id.header_backtext);
		tvTitle = (TextView)findViewById(R.id.header_title);
		
		if(bnBackVisible == true)
		{
			if(backText != null)
			{
				tvBack.setText(backText);
			}
			
			bnBack.setVisibility(View.VISIBLE);
			tvBack.setVisibility(View.VISIBLE);
		}
		else
		{
			bnBack.setVisibility(View.GONE);
			tvBack.setVisibility(View.GONE);
		}
		
		tvTitle.setText(titleText);
		
		if(titleColor != null)
		{
			tvTitle.setTextColor(titleColor.getDefaultColor());
		}
		
		bnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(onBackButtonListener != null)
				{
					onBackButtonListener.onButtonClick(v);
				}
			}
		});
	}
	
	public void setTitle(String text)
	{
		tvTitle.setText(text);
	}
	
	public ImageButton getBackButton()
	{
		return bnBack;
	}
	
	public void setOnBackButtonClickListener(OnBackButtonClickListener listener)
	{
		onBackButtonListener = listener;
	}
	
	/**
	 * 返回按钮点击监听接口
	 * @author daiyu
	 *
	 */
	public interface OnBackButtonClickListener extends EventListener{
		
		abstract public void onButtonClick(View v);
	}
}
